home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1993…ch: Other People's Memory / ADC Developer CD (1993-03) (''Other People's Memory'')_iso / Dev.CD Mar 93.iso / Technical Documentation / Sample Code / Snippets / Files / ParseFullPathname / ParseFullPathname.p < prev    next >
Encoding:
Text File  |  1993-01-18  |  4.0 KB  |  118 lines  |  [TEXT/MPS ]

  1. {
  2.  
  3. ParseFullPathname
  4.     
  5.     Takes a full pathname and turns it into a DirID and VRefNum pair.
  6.     
  7.     This does not handle partial pathnames because it assumes that the first
  8.     element in the pathname is the volume name.
  9.     
  10. }
  11.  
  12. Program ParsePath;
  13.  
  14. uses
  15.     Files,Memory,Errors,IntEnv;
  16.  
  17. const
  18.     noError = 0;
  19.  
  20. type
  21.     longString = record
  22.         length : integer;
  23.         text : Ptr;
  24.     end;
  25.     
  26. var
  27.     theError        : OSErr;
  28.     here            : FSSpec;
  29.     theInputString    : longString;
  30.     tempByte        : Byte;
  31.  
  32.  
  33.  
  34. function GetElement(theElement: StringPtr; thePathname: longString; elementIndex: integer): boolean;
  35. var
  36.     elemStart, elemEnd    : longint;
  37. begin
  38.     elemStart := longint(thePathname.text);
  39.     while (elementIndex > 1) and (elemStart <
  40.                 longint(thePathname.text) + thePathname.length) do begin            { Loop to find start of desired element }
  41.         if Ptr(elemStart)^ = ord(':') then                                            { ":" means we found an element delimiter }
  42.             elementIndex := elementIndex - 1;                                        { If so, elementIndex - 1 elements to go }
  43.         elemStart := elemStart + 1;                                                    { Increment for next character in string }
  44.     end;
  45.     elemEnd := elemStart;                                                            { Begin looking at start of desired element }
  46.     while (Ptr(elemEnd)^ <> ord(':')) and (elemEnd <
  47.                 longint(thePathname.text) + thePathname.length) do                     { Loop to find ":" at end of desired element }
  48.         elemEnd := elemEnd + 1;
  49.     if (elemEnd > elemStart) and (elemEnd < elemStart + 32) then begin                { If the above looped at least once, then }
  50.         BlockMove(Ptr(elemStart),Ptr(longint(theElement) + 1),elemEnd - elemStart);    {  copy the string element into theElement }
  51.         theElement^[0] := char(elemEnd - elemStart);                                { Copy in the length of the string }
  52.         GetElement := true;
  53.     end else                                                                        { If the element's length is 0, routine fails }
  54.         GetElement := false;
  55. end;
  56.  
  57.  
  58. function ParseFullPathName(VAR theResult: FSSpec; thePathname: longString) : OSErr;    { Walk down thePath, returning an FSSpec }
  59.  
  60. var
  61.     theOSError        : OSErr;
  62.     theLevel        : integer;
  63.     theDInfo        : CInfoPBRec;
  64.     theVInfo        : HParamBlockRec;
  65.     
  66. begin
  67.     theLevel := 2;
  68.     ParseFullPathName := noError;                                                    { And remains so unless an error occurs in here }
  69.     
  70.     theResult.parID := fsRtDirID;                                                    { Start at the root directory }
  71.     
  72.     if GetElement(StringPtr(@theResult.name), thePathname, 1) then begin            { If there's a valid volume name, }
  73.         with theVInfo do begin
  74.             ioVRefNum := 0;                                                            { get its vRefNum… }
  75.             ioNamePtr := Pointer(@theResult.name);
  76.             ioVolIndex := -1;
  77.         end;
  78.         theError := PBHGetVInfo(@theVInfo,false);
  79.         if theError <> 0 then
  80.             ParseFullPathName := theError                                            { and return an error if path not found }
  81.         else
  82.             theResult.vRefNum := theVInfo.ioVRefNum;                                { Otherwise, save the valid vRefNum }
  83.     end;
  84.     
  85.     while GetElement(StringPtr(@theResult.name), thePathname, theLevel) do begin    { Now, walk down the path, }
  86.         with theDInfo do begin
  87.             ioFDirIndex := 0;                                                        { and get its DirID }
  88.             ioDrDirID := theResult.parID;
  89.             ioVRefNum := theResult.vRefNum;
  90.             ioNamePtr := Pointer(@theResult.name);
  91.         end;
  92.         theError := PBGetCatInfo(@theDInfo, false);
  93.         if theError <> 0 then
  94.             ParseFullPathName := theError                                            { Return an error if path not found }
  95.         else
  96.             theResult.parID := theDInfo.ioDrDirID;                                    { Otherwise, save the valid path info }
  97.         theLevel := theLevel + 1;                                                    { Increment looping variable to go deeper }
  98.     end;                                                                            { This loop repeats to the bottom of the path }
  99. end;
  100.  
  101.  
  102.  
  103. begin    { main program }
  104.     if argC <> 2 then writeln('Form: ParseFullPathname <pathname>')                    { Educate the user, if necessary }
  105.     else begin
  106.         tempByte := Ptr(argV^[1])^;
  107.         theInputString.length := tempByte;
  108.         theInputString.text := Ptr(longint(argV^[1])+1);
  109.         theError := ParseFullPathName(here,theInputString);                            { Call ParsePath on full pathname string }
  110.         if theError <> noError then
  111.             writeln('Failed on error ',theError,'.')                                { Return the error from PBHGetVInfo or PBGetCatInfo }
  112.         else begin
  113.             writeln('  vRefNum: ',here.vRefNum);                                    { or write out the results }
  114.             writeln('    DirID: ',here.parID);
  115.             writeln('     Name: ',here.name);
  116.         end;
  117.     end;
  118. end.    { main program }